package utils

/**
* @program: fileshare
*
* @description:日志信息打印
*
* @author: Koco.Allien
*
* @create: 2020-04-05 16:09
**/

import (
	"fileshare/conf"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/gin-gonic/gin"
)

var MyLogconf gin.LoggerConfig
var MyLog *log.Logger

//设置日志输出
func SetLog(outputfile bool) {
	//初始化MyLogconf
	MyLogconf = gin.LoggerConfig{
		Formatter: nil,
		Output:    nil,
		SkipPaths: nil,
	}

	//设置日志的格式
	log.SetPrefix(conf.Cfg.Key("appname").MustString("FileShare(koco)"))
	log.SetFlags(log.LstdFlags)

	//日志输出的未知
	if outputfile {
		logpath := conf.Cfg.Key("logpath").MustString("./logs/")
		tm := time.Now().Format("2006-01-02.log")
		logpath = filepath.Join(logpath, tm)

		f, err := os.OpenFile(logpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm)
		if err != nil {
			ERRORToLog("设置日志输出到文件失败", err)
		} else {
			log.SetOutput(f)
			MyLogconf.Output = f
			MyLog = log.New(f, "\r\n", 0)
		}
	} else {
		MyLog = log.New(os.Stdout, "\r\n", 0)
	}
	MsgToLog("设置日志输出成功")
}

//MSG   =   //运行的状态的提示信息
func MsgToLog(msg string, strs ...interface{}) {
	logstr := "[MSG] : " + msg
	log.Println(logstr, strs)
}

//ERROR = "[ERROR]" //出现错误
func ERRORToLog(msg string, strs ...interface{}) {
	logstr := "[ERROR] : " + msg
	log.Println(logstr, strs)
}

//LOG   = "[LOG]"   //需要打印的参数的信息
func LOGToLog(msg string, strs ...interface{}) {
	logstr := "[LOG] : " + msg
	log.Println(logstr, strs)
}
